import { createSlice } from '@reduxjs/toolkit';
import { getMenuList } from '@/api/user';

const menuStore = createSlice({
  name:'menu',
  initialState: {
    menuList: [],
  },
  reducers: {
    setMenuItems: (state, action) => {
      state.menuList = action.payload;
    },
  },
});


export const { setMenuItems } = menuStore.actions;

export const setMenuList = () => {
  return async (dispatch) => {
    const response = await getMenuList();
    const data = response.data;
    localStorage.setItem('menuList', JSON.stringify(data));
    dispatch(setMenuItems(data));
  };
};

const menuReducer = menuStore.reducer;

export default menuReducer